<?php

/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoa_Convertor
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoathis_Convertor_Exception
 */
importModule('Convertor.Exception');

/**
 * Class Hoathis_Convertor_Filter_Abstract
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Gregory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Convertor
 * @subpackage  Hoathis_Convertor_Filter
 */
abstract class Hoathis_Convertor_Filter_Abstract {

    /**
     * Param Array
     */
    protected $param = array();

    /**
     * Value being converted
     */
    public $value = null;

    /**
     * Converted value
     */
    public $_value = null;

    /**
     * Constructor
     *
     * @param	string	value	Value being filtered
     * @param	string 	param   Parameters
     */
    public function __construct( $value = null, $param = array() ) {

        if (is_null($value))
            throw new Hoathis_Convertor_Exception('ne peut pas etre null', 2);

        if (is_null($param) AND !is_array($param))
            throw new Hoathis_Convertor_Exception('ne peut pas etre null et doit être un array', 2);

        $this->param = array_merge($this->param, $param);
        $this->_value = $this->filter(trim($value));
    }

    /**
     * filter
     *
     * @param	string	value	Value being converted
     * @return	mixte			Converted value
     */
    abstract protected function filter( $value );

    /**
     * __tostring
     *
     * @return	string	formatted value
     */
    public function __tostring() {

        return (string) $this->_value;
    }

    /**
     * param_exist
     *
     * @return	string	param	Param to test if exist
     * @return	boolean			Return TRUE if the param exist
     */
    private function param_exist( $param = null ) {

        if (is_null($param) AND !is_string($param))
            throw new Hoathis_Convertor_Exception('the param "'.$param.'" is invalid', 2);

        return array_key_exists($param, $this->$params);
    }
}